package com.xgpro.mobile.message.service.impl;

import com.github.pagehelper.PageHelper;
import com.xgpro.domain.messagemanage.entity.Message;
import com.xgpro.mapper.message.MessageMapper;
import com.xgpro.mobile.message.service.MessageService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @program: xgpro__dev
 * @description: 消息服务实现类
 * @author: likai
 * @create: 2019-09-27 02:08
 **/
@Service
@Transactional
public class MessageServiceImpl implements MessageService {


    @Resource
    private MessageMapper messageMapper;
    @Override
    public List<Message> queryList(int pageNum, int pageSize, String isRead, Integer userId) {
        PageHelper.startPage(pageNum,pageSize);
        List<Message> messages = messageMapper.queryList(userId, isRead,null);

        return messages;
    }

    @Override
    public Message queryOneNewMessage(String isRead, Integer userId) {
        Message message = null;
        List<Message> limit = messageMapper.queryList(userId, isRead, "limit");
        if (!CollectionUtils.isEmpty(limit)){
            message = limit.get(0);
        }

        return message;
    }

    @Override
    public int insert(Message message) {
        return messageMapper.insert(message);
    }

    @Override
    public int delete(Integer msgId) {
        return messageMapper.deleteByPrimaryKey(msgId);
    }

    @Override
    public int sendInform(Integer userId, Integer msgId) {
        return messageMapper.sendInform(userId,msgId);
    }

    @Override
    public Message findOne(Integer msgId) {
        return messageMapper.selectByPrimaryKey(msgId);
    }

    @Override
    public int updateActive(Integer id,Integer userId) {

        return messageMapper.updateActive(id,userId);
    }

    @Override
    public int getCount(Integer userId, String isRead) {

        return messageMapper.getCount(userId,isRead);
    }

    @Override
    public int deleteByTime(String format) {
        return messageMapper.deleteByTime(format);
    }
}
